﻿using System.Windows;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 保存当前所有文档。
        /// </summary>
        /// <returns>成功返回true。</returns>
        public static bool SaveAllFiles()
        {
            if (Globals.selectedEditor == null) return false;

            System.Windows.Controls.TabControl mainTabControl
                = Globals.selectedEditor.ParentWindow.mainTabControl;

            foreach (TabItem ti in mainTabControl.Items)
            {
                Editor editor = ti as Editor;
                if (editor != null)
                {
                    if (editor.IsModified == false) continue;

                    if (SaveFile(editor) == false)
                    {
                        System.Windows.MessageBox.Show(
                            "发生意外，未能完成【保存所有文档】这一操作！\r\n这可能是因为指定了空文件名或重复的文件名。",
                            Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                        return false;
                    }
                }
            }

            return true;
        }
    }
}
